//
//  CJBZInterstitialAdapter.m
//  Pods
//
//  Created by shanghailingye on 2025/10/20.
//

#import "CJBZInterstitialAdapter.h"

@interface CJBZInterstitialAdapter()<CJInterstitialAdDelegate>

@property (nonatomic, strong) CJInterstitialAd *interstitial;

@property (nonatomic, assign) BOOL isLoadFailHasCallBack;

@property (nonatomic, assign) BOOL isBiddingRequest;

@property (nonatomic, strong) AMPSAdConfiguration *adConfiguration;

@end

@implementation CJBZInterstitialAdapter

/// 竟胜回传
- (void)sendWinNotificationWithInfo:(NSDictionary *)winInfo {
    
}

/// 竞负回传
- (void)sendLossNotificationWithInfo:(NSDictionary *)lossInfo {
    
}

- (void)requestBiddingPriceWithSpaceId:(NSString *)spaceId
                       adConfiguration:(AMPSAdConfiguration *)adConfiguration {
    self.isBiddingRequest = YES;
    [self loadAdWithSpaceId:spaceId adConfiguration:adConfiguration];
}

- (void)loadAdWithSpaceId:(nonnull NSString *)spaceId adConfiguration:(nonnull AMPSAdConfiguration *)adConfiguration {
    if (CJBZSDKManagerAdapter.initState != kAdScopeMediationAdapterInitSDKStateSuccess) {
        __weak typeof(self)weakSelf = self;
        [CJBZSDKManagerAdapter startAsyncWithAppId:self.appId configuration:self.sdkConfiguration results:^(BOOL adapterResult) {
            __strong typeof(weakSelf)strongSelf = weakSelf;
            if (adapterResult) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [strongSelf lingYeLoadAdWithSpaceId:spaceId adConfiguration:adConfiguration];
                });
            } else {
                if (strongSelf.isBiddingRequest) {
                    NSError *error = [NSError errorWithDomain:@"xyz.adscope.MediationErrorDomain" code:250401 userInfo:@{@"NSLocalizedDescription": @"AMPSGDTSDKManagerAdapter init fail"}];
                    if (strongSelf.bidDelegate && [strongSelf.bidDelegate respondsToSelector:@selector(requestBiddingPriceFail:error:ext:)]) {
                        [strongSelf.bidDelegate requestBiddingPriceFail:strongSelf error:error ext:adConfiguration.ext];
                    }
                } else {
                    NSError *error = [NSError errorWithDomain:@"xyz.adscope.MediationErrorDomain" code:270401 userInfo:@{@"NSLocalizedDescription": @"AMPSGDTSDKManagerAdapter init fail"}];
                    if (strongSelf.customDelegate && [strongSelf.customDelegate respondsToSelector:@selector(interstitialAdLoadFail:error:ext:)]) {
                        [strongSelf.customDelegate interstitialAdLoadFail:strongSelf error:error ext:adConfiguration.ext];
                    }
                }
            }
        }];
    } else {
        [self lingYeLoadAdWithSpaceId:spaceId adConfiguration:adConfiguration];
    }
}

- (void)lingYeLoadAdWithSpaceId:(NSString *)spaceId adConfiguration:(AMPSAdConfiguration *)adConfiguration {
    self.interstitial = [[CJInterstitialAd alloc] initWithResourceId:spaceId];
    self.interstitial.delegate = self;
    [self.interstitial loadAdData];
}


- (void)removeAd {
    if (_interstitial) {
        if ([_interstitial respondsToSelector:@selector(delegate)]) {
            _interstitial.delegate = nil;
        }
        _interstitial = nil;
    }
}

- (BOOL)isValid {
    return self.isLoadAdSuccess;
}

- (void)showInterstitialAdInViewController:(nonnull UIViewController *)viewController {
    [self.interstitial showFromRootViewController:viewController];
}

- (void)loadC2SAdWithSpaceId:(NSString *)spaceId
                biddingToken:(id)biddingToken
             adConfiguration:(AMPSAdConfiguration *)adConfiguration {
    if (self.isLoadAdSuccess) {
        if (self.customDelegate && [self.customDelegate respondsToSelector:@selector(interstitialAdLoadSuccess:ext:)]) {
            [self.customDelegate interstitialAdLoadSuccess:self ext:self.adConfiguration.ext];
        }
    }
}

- (void)loadS2SAdWithSpaceId:(NSString *)spaceId
                biddingToken:(id)biddingToken
             adConfiguration:(AMPSAdConfiguration *)adConfiguration {
    
}

- (NSInteger)eCPM {
    return [self.interstitial getECpm];
}

- (void)interstitialAdDidLoad:(CJInterstitialAd *)interstitialAd resourceId:(NSString *)resourceId {
    self.isLoadAdSuccess = YES;
    if (!self.isBiddingRequest) {
        if (self.customDelegate && [self.customDelegate respondsToSelector:@selector(interstitialAdLoadSuccess:ext:)]) {
            [self.customDelegate interstitialAdLoadSuccess:self ext:self.adConfiguration.ext];
        }
        return;
    }
    if ([self.interstitial getECpm] > 0) {
        if (self.bidDelegate && [self.bidDelegate respondsToSelector:@selector(requestBiddingPriceSuccess:ext:)]) {
            [self.bidDelegate requestBiddingPriceSuccess:self ext:self.adConfiguration.ext];
        }
    } else {
        if (self.bidDelegate && [self.bidDelegate respondsToSelector:@selector(requestBiddingPriceFail:error:ext:)]) {
            NSError *error = [NSError errorWithDomain:@"xyz.adscope.MediationErrorDomain" code:250401 userInfo:@{@"NSLocalizedDescription": @"AMPSGDTInterstitialAdapter ad bidding fail,ecpm less than 0"}];
            [self.bidDelegate requestBiddingPriceFail:self error:error ext:self.adConfiguration.ext];
        }
    }
}

- (void)interstitialAdLoadFailed:(CJInterstitialAd *)interstitialAd error:(NSError *)error {
    if (!_isLoadFailHasCallBack) {
        _isLoadFailHasCallBack = YES;
        if (self.isBiddingRequest) {
            if (self.bidDelegate && [self.bidDelegate respondsToSelector:@selector(requestBiddingPriceFail:error:ext:)]) {
                [self.bidDelegate requestBiddingPriceFail:self error:error ext:self.adConfiguration.ext];
            }
        } else {
            if (self.customDelegate && [self.customDelegate respondsToSelector:@selector(interstitialAdLoadFail:error:ext:)]) {
                [self.customDelegate interstitialAdLoadFail:self error:error ext:self.adConfiguration.ext];
            }
        }
        [self removeAd];
    }
}


- (void)interstitialAdOnShow:(CJInterstitialAd *)interstitialAd {
    if (self.customDelegate && [self.customDelegate respondsToSelector:@selector(interstitialAdDidShow:)]) {
        [self.customDelegate interstitialAdDidShow:self];
    }
}

- (void)interstitialAdOnClicked:(CJInterstitialAd *)interstitialAd {
    if (self.customDelegate && [self.customDelegate respondsToSelector:@selector(interstitialAdDidClick:)]) {
        [self.customDelegate interstitialAdDidClick:self];
    }
}

- (void)interstitialAdOnClosed:(CJInterstitialAd *)interstitialAd {
    if (self.customDelegate && [self.customDelegate respondsToSelector:@selector(interstitialAdDidClose:)]) {
        [self.customDelegate interstitialAdDidClose:self];
    }
    [self removeAd];
}

- (void)dealloc {
    [self removeAd];
}


@end
